import { PUSHITEM } from "../actionTypes";
const initState = {
  list: [
    { id: 1, label: "苹果" },
    { id: 2, label: "香蕉" },
    { id: 3, label: "葡萄" },
    { id: 4, label: "芒果" }
  ]
};
// state是只读的，深拷贝后在修改
export default (state = initState, action) => {
  // 深拷贝
  let newState = JSON.parse(JSON.stringify(state));
  // 业务
  switch (action.type) {
    // 1.添加一条新数据
    case PUSHITEM:
      if (action.value) {
        const label = action.value;
        const id = newState.list[newState.list.length - 1].id;
        newState.list.push({
          id: id + 1,
          label
        });
      }

      return newState;
    // 默认返回;
    default:
      return state;
  }
};
